package io.gitee.welson.sensitive.strategy.impl;

import io.gitee.welson.sensitive.annotation.SensitiveStrategyComponent;
import io.gitee.welson.sensitive.strategy.SensitiveStrategy;
import io.gitee.welson.sensitive.util.SensitiveUtil;

/**
 * 邮箱脱敏策略
 * 规则：保留前3位和@后面的所有内容，中间用*代替
 *
 * @author welson
 */
@SensitiveStrategyComponent
public class EmailSensitiveStrategy implements SensitiveStrategy {

    private static final int MIN_LENGTH = 7;
    private static final int KEEP_PREFIX = 3;

    @Override
    public String desensitize(String value, String... params) {
        if (SensitiveUtil.isInvalid(value, MIN_LENGTH)) {
            return value;
        }

        int atIndex = value.indexOf('@');
        if (atIndex <= 0) {
            return value;
        }

        // 获取用户名和域名部分
        String username = value.substring(0, atIndex);
        String domain = value.substring(atIndex);

        // 如果用户名长度小于等于3，直接返回原值
        if (username.length() <= KEEP_PREFIX) {
            return value;
        }

        // 对用户名部分进行掩码处理
        return SensitiveUtil.mask(username, KEEP_PREFIX, 0) + domain;
    }
} 